PROJECTENGRAM

CODING STANDARDS

Coding Standards

Tags: #◇Conceptual #◇CCPROG1 #◇Best Practices #◇M8


◇Overview

Coding standards are a set of rules, guidelines, and best practices that developers follow when writing code. Consistently applying these standards makes code more readable, maintainable, and understandable, both for the original author and for others.

◇Key Definitions

Naming Conventions: Rules for naming variables, functions, and files (e.g., camelCase, snake_case). Indentation: Spaces or tabs used to format code structure visually. Documentation/Comments: Written explanations in the code to clarify logic (// or /* */).

◇Theoretical Framework

Importance of Coding Standards

  1. Readability: Clean, well-formatted code is easier to read.
  2. Maintainability: Standardized code is easier to update and debug later.
  3. Collaboration: When multiple programmers work on the same project, a shared standard ensures the codebase looks cohesive.

Common Guidelines in C Meaningful Names: Use descriptive names (total_sales instead of ts). Consistent Bracing: Choose a bracing style (e.g., K&R or Allman) and stick to it.

『』
C
// K&R Style
if (condition) {
    // code
}
``` **Proper Indentation:** Use 4 spaces (or 1 tab) for each level of block nesting. **Comments:** Comment on *why* something is being done rather than *what* is being done, unless the what is highly complex. **Avoid Magic Numbers:** Use `#define` or `const` for raw numbers to give them context (e.g., `#define MAX_STUDENTS 50`).

## Common Pitfalls
Writing excessively clever or compact code that becomes impossible for others to read. Using inconsistent naming conventions (mixing `CamelCase`, `snake_case`, and `lowercase`). Over-commenting obvious code (e.g., `i++; // increments i by 1`).

## References & Navigation

- **Previous Topic:** [[Testing and Debugging]]
- **Related Notes:** [[Testing and Debugging]], [[Design Principles]]
CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Why do C coding standards strongly discourage the use of "magic numbers" (unnamed literal constants like if (status == 42)) in source code?

Q2

Which of the following identifier pairs follows standard C naming conventions for distinguishing a symbolic constant/macro from a local variable?

Q3

What is the primary purpose of writing function header comments (docstrings) in a C program?

Q4

How does the C compiler treat indentation and whitespace outside of string literals and preprocessor directives?

Q5

Which practice best adheres to modular coding standards when writing functions in C?